package org.shnux.android.bingo;

import com.admob.android.ads.AdView;
import com.admob.android.ads.AdManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import android.widget.TextView;
import android.widget.ToggleButton;
import java.util.*;

public class GameActivity extends Activity {
	private static final String FILENAME = "hello_file10";
	public String s= "r1b1";
	public int k=0;
	public TextView txt1;    
	public int[][] row = new int[5][5];
	public String fulltext="";
	public String ft="";
	
	public String lv_arr[]={"analogous","ball park","benchmark","best practice","blame culture","bottom line","challenge","implement","differential","diversity","empower","envision","fast track","hardball","leverage","metrics","paradigm","performance","pinch point","proactive","revisit","strategy","timeline","clarify on","robust","focus","revisit","silo","pinch point","touch base","core business","mindset","networking","envision","strategic fit","core business","diversity","timeline","metrics","touch base","cascade","win-win","historic","inherited","scale","the bubble","wow factor","market leader","touch base","prioritized","alignment","dynamic","brand(ing)"};
	public String[] fulltextarray;
	ArrayList<Integer> numbers = new ArrayList<Integer>();
	public static Context context;
	
	

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        
       // AdManager.setTestDevices( new String[] {
        //		AdManager.TEST_EMULATOR, // Android emulator
        //		"E83D20734F72FB3108F104ABC0FFC738", // My T-Mobile G1 Test Phone
        //		} );
        //AdView adview= (AdView) findViewById(R.id.ad);
       // adview.requestFreshAd();
        
        initApp();
 
        fulltextarray=fulltext.split(",");
        
        for (int n=0;n<fulltextarray.length;n++){
        	numbers.add(n);
        }
    	
    	row[0][0]=0;
		row[1][0]=1;
		row[2][0]=0;
		row[3][0]=0;
		
		//txt1 = (TextView) findViewById(R.id.txt1);

        for( int i=1;i<5;i++){
        	for ( int j=1;j<5;j++){
        		
        		//init array
        		
        			
        		
			       int butID = getResources().getIdentifier("r"+i+"b"+j, "id", "org.shnux.android.bingo");                   
			       final  ToggleButton button = (ToggleButton) findViewById(butID);
			       			
			       			
			       			//int run= (int) (Math.random() * fulltextarray.length);
			       			//numbers.add(run);
			       			int run=getRandom();
			       			
			       			button.setTextOn(fulltextarray[run]);
			       			button.setTextOff(fulltextarray[run]);
			       			button.setText(fulltextarray[run]);
			       			k=k+1;
			       			Point obj=new Point(i,j);
			       			//button.setTag(1,obj);
			       			button.setTag(obj);
			         		button.setOnClickListener(new View.OnClickListener() {
				             public void onClick(View v) {
				            	 Point obj1=(Point) button.getTag();
				            	 int i1 = obj1.i;
				            	 int j1 = obj1.j;
				            	 
				            	 if(button.isChecked()){
				            		 button.setChecked(true);
				            		 row[i1][j1]=1;
				            	 }else{
				            		 button.setChecked(false);
				            		 row[i1][j1]=0;
				            	 }
				            	 
				            	 
				            	//txt1.setText("aaa"+row[1][0]+"aaa");
				            	// txt1.setText(".");
				                 for( int x=1;x<5;x++){
				                	int xcount=0;
				                	int ycount=0;
				                	int dxcount=0;
				                	int dycount=0;
				                 	for ( int y=1;y<5;y++){
				                 		   
				                 		if(row[x][y]==1) xcount=xcount+1;
				                 		if(row[y][x]==1) ycount=ycount+1;
				                 		
				                 	}
				                 	for ( int z=1;z<5;z++){
				                 		   
				                 		if(row[z][z]==1) dxcount=dxcount+1;
				                 		if(row[5-z][z]==1) dycount=dycount+1;
				                 		
				                 	}
				                 	
				                 	if(xcount==4 || ycount==4 || dxcount==4 || dycount==4) {
				                 		onBingo();
				                 		break;
				                 	}
				                 	xcount=0;
				                 	ycount=0;
				                 }	
				            	 
				            	 
				            	 
				            	 
				            	 
				             }
			         });
			         		

        		
        	}
        
        }
        k=0;
        
        /*        
        final Button button = (Button) findViewById(R.id.button_id);

        
        
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            }
        });
*/
        

    }
	
    void onBingo(){ 
    	finish();
    	overridePendingTransition(R.anim.moveout, R.anim.moveout);
    	//overridePendingTransition(R.anim.fade_in, R.anim.fade_in);
        Intent intent = new Intent(getApplicationContext(), BingoActivity.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        
    	startActivity(intent);
    }
    
	class Point {
	      public int i = 0;
	      public int j = 0;
	    //constructor
	    public Point(int a, int b) {
			i = a;
			j = b;
	    }
	    public int geti(){
	    	return i;
	    }
	    public int getj(){
	    	return j;
	    }
	}
	
	private void initApp(){
		//
        context = getApplicationContext();
        
        File file = context.getFileStreamPath(FILENAME);
        if(!file.exists()){	    	         	        	
        		  ft="";   
        		  fulltext="";
    		    	  for (int j=0;j<lv_arr.length;j++){
    		    		  
    		    		 ft=ft+lv_arr[j]+"\n"; 
    		    		 fulltext=fulltext+lv_arr[j].replace(" ", "\n")+",";
    		    		 
    		    		 
    		    	  }    		      
    		    	  fulltext=fulltext.substring(0, fulltext.length()-1);
    		    	  
	    	  	FileOutputStream fos = null; 	    
	    	  	try {
					fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
					fos.write(ft.getBytes());										
					fos.close();
					
					
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
		     
        }else{
        	try {
    		    // open the file for reading
    			
    		    FileInputStream instream = openFileInput(FILENAME);
    		 
    		    // if file the available for reading
    		    
    		      // prepare the file for reading
    		      InputStreamReader inputreader = new InputStreamReader(instream);
    		      BufferedReader buffreader = new BufferedReader(inputreader);
    		 
    		      String line;
    		 
    		      fulltext="";
    		      while (( line = buffreader.readLine()) != null ) {
    		    	  line=line.replace(" ", "\n");
    		    	  fulltext=fulltext+line+",";	
    		    	  
    		      }
    		      fulltext=fulltext.substring(0, fulltext.length()-1);
    		 
    		    instream.close();
    		  } catch (java.io.FileNotFoundException e) {
    		    // do something if the myfilename.txt does not exits
    		  } catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace(); 
    		}
        	
        }
      
		
	}
    public void onBackPressed() { 
  	  
  	  this.finish();
        Intent intent = new Intent(getApplicationContext(), MenuActivity.class); 
        //intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    	  startActivity(intent);
    	
  	  overridePendingTransition(R.anim.moveout, R.anim.moveout);
  	}
	int getRandom(){ 
		
		

		int run= (int) (Math.random() * numbers.size() );
			
		int out=numbers.get(run);
			
			//numbers.indexOf(run)
		
		
			return out; 

	}

    
}